using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MobileStreams
{
    public partial class frmNewEditChannel : Form
    {
        private bool IsEditMode = false;
        private Channel _EditChannel = null;
        private int _EditChannelIndex;

        private Channel[] customChannels;

        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="EditChannel">If != null program is in editmode for specified channel</param>
        public frmNewEditChannel(Channel EditChannel)
        {
            customChannels = XMLHandling.getChannels(XMLHandling.strAppDir + "\\CustomContent.xml");

            // Switch to edit-mode
            if (EditChannel != null)
            {
                IsEditMode = true;
                _EditChannel = EditChannel;
                this.Text = string.Format("Edit Channel {0}", _EditChannel.ChannelName);

                // Search
                for (int i = 0; i < customChannels.Length; i++)
                {
                    if (customChannels[i] == _EditChannel)
                    {
                        _EditChannelIndex = i;
                        break;
                    }
                }
            }

            InitializeComponent();
        }

        private void frmNewEditChannel_Load(object sender, EventArgs e)
        {
            if (IsEditMode) DisplayEditChannelData();
        }

        /// <summary>
        /// Displays the details for the channel that should be edited
        /// </summary>
        private void DisplayEditChannelData()
        {
            tbDescription.Text = _EditChannel.Description;
            tbName.Text = _EditChannel.ChannelName;

            if (_EditChannel.Streams.Length > 0)
            {
                tbURL.Text = _EditChannel.Streams[0].URL;
                tbURLNotes.Text = _EditChannel.Streams[0].Description;            
            }
        }

        private void menuitemSave_Click(object sender, EventArgs e)
        {
            if (IsEditMode)
            { UpdateChannel(); }
            else
            { CreateChannel(); }
        }

        private void menuitemCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CreateChannel()
        {
            Channel newChan = SetChannelProperties();

            Channel[] NewChannels = new Channel[customChannels.Length + 1];

            customChannels.CopyTo(NewChannels, 0);
            NewChannels[customChannels.Length] = newChan;

            XMLHandling.WriteChannels(NewChannels, XMLHandling.strAppDir + "\\CustomContent.xml");
            
            this.Close();
        }

        private Channel SetChannelProperties()
        {
            Channel newChan = new Channel();
            Stream newStream = new Stream();

            newChan.ChannelName = tbName.Text;
            newChan.Description = tbDescription.Text;
            newChan.ChannelLogo = "Custom.jpg";
            newChan.Category = 999;
            newChan.ID = 0;
            newChan.URL = ""; // TODO

            newStream.URL = tbURL.Text;
            newStream.Description = tbURLNotes.Text;

            newChan.Streams = new Stream[] { newStream };

            return newChan;
        }

        private void UpdateChannel()
        {
            customChannels[_EditChannelIndex] = SetChannelProperties();

            XMLHandling.WriteChannels(customChannels, XMLHandling.strAppDir + "\\CustomContent.xml");

            this.Close();
        }
    }
}